import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    num: 23
  },
  mutations: {
    addOneNum(state) {
      state.num += 1
    },
    addNNum(state, n) {
      state.num += n
    },
    subOneNum(state) {
      state.num -= 1
    },
    subNNum(state, n) {
      state.num -= n
    }
  },
  actions: {
    addOneNumAsync(context) {
      setTimeout(_ => {
        context.commit('addOneNum')
      }, 1000)
    },
    addOneNAsync(context, n) {
      setTimeout(_ => {
        context.commit('addNNum', n)
      }, 1000)
    },
    subOneNumAsync(pang) {
      setTimeout(() => {
        pang.commit('subOneNum')
      }, 1000)
    },
    subNNumAsync(context, n) {
      setTimeout(()=>{
        context.commit('subNNum', n)
      },1000)
    }
  },
  getters: {
    infoNum: state => {
      return state.num + '你真好'
    }
  }
})
